import 'package:flutter/material.dart';

import 'pledge_page.dart';
import 'zero_interest_page.dart';

class LoanPage extends StatefulWidget {
  @override
  _LoanPageState createState() => _LoanPageState();
}

class _LoanPageState extends State<LoanPage>
    with SingleTickerProviderStateMixin {
  int _selectIndex = 0;
  bool _isOnTab;
  TabController _tabController;
  PageController _pageController = PageController();
  List<String> titles = ["质押借贷", "零息借贷"];
  List fixedList;

  @override
  void initState() {
    super.initState();
    fixedList = Iterable<int>.generate(titles.length).toList();
    _tabController = TabController(length: titles.length, vsync: this);
  }

  //切换页面的方法
  _onTabPageChange(index, {bool isOnTab = false}) {
    if (_selectIndex == index) {
      return;
    }
    setState(() {
      _selectIndex = index;
    });

    if (!isOnTab) {
      _tabController.animateTo(_selectIndex);
    } else {
      _pageController.animateToPage(_selectIndex,
          duration: Duration(milliseconds: 200), curve: Curves.linear);
      //等待滑动解锁
      Future.delayed(Duration(milliseconds: 200), () {
        _isOnTab = false;
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          brightness: Brightness.light,
          iconTheme: IconThemeData(
            color: Colors.black, //修改颜色
          ),
          backgroundColor: Colors.transparent,
          elevation: 0,
          title: Text(""),
          centerTitle: true,
          actions: [
            TextButton(
                onPressed: () {Navigator.pushNamed(context, '/loan/my');},
                child: Text(
                  '我的订单',
                  style: TextStyle(color: Colors.black),
                ))
          ],
        ),
        body: SafeArea(
            child: Column(children: <Widget>[
          Container(
              width: 240,
              height: 32,
              decoration: new BoxDecoration(
                  color: Color(0xFFE6EBFE),
                  //设置四周圆角 角度
                  borderRadius: BorderRadius.all(Radius.circular(28.0))),
              child: Row(
                  children: fixedList.map<Widget>((index) {
                return Expanded(
                  flex: 1,
                  child: InkWell(
                    child: Container(
                        decoration: BoxDecoration(
                            color: _selectIndex == index
                                ? Colors.blueAccent
                                : Colors.transparent,
                            //设置四周圆角 角度
                            borderRadius:
                                BorderRadius.all(Radius.circular(28.0)),
                            boxShadow: _selectIndex == index
                                ? [
                                    BoxShadow(
                                        color: Colors.black12,
                                        offset: Offset(0.0, 5.0), //阴影xy轴偏移量
                                        blurRadius: 5.0, //阴影模糊程度
                                        spreadRadius: 1.0 //阴影扩散程度
                                        )
                                  ]
                                : null),
                        child: Center(
                          child: Text(
                            titles[index],
                            style: TextStyle(
                                fontSize: 16,
                                color: _selectIndex == index
                                    ? Colors.white
                                    : Colors.black45),
                          ),
                        )),
                    onTap: () {
                      _isOnTab = true;
                      _onTabPageChange(index, isOnTab: true);
                    },
                  ),
                );
              }).toList())),
          Expanded(
            child: PageView(
              onPageChanged: (index) {
                if (!_isOnTab) {
                  _onTabPageChange(index, isOnTab: false);
                }
              },
              controller: _pageController,
              children: <Widget>[
                PledgePage(),
                ZeroInterestPage(),
              ],
            ),
          )
        ])));
  }
}
